package cn.com.dhcc.turbo.service.transport;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.entity.exch.CmData;
import cn.com.dhcc.turbo.entity.exch.CrossNetTmpCmData;
import cn.com.dhcc.turbo.entity.exch.QueueMsgWrapper;
import cn.com.dhcc.turbo.entity.exch.RecvCmData;
import cn.com.dhcc.turbo.entity.exch.SendCmData;
import cn.com.dhcc.turbo.service.queue.QueueService;

@Component
public class TlqTransportPipeline implements TransportPipeline {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.R);
	
	@Autowired
	@Qualifier(IocBean.TLQ_QUEUE_SERVICE_BEAN)
	private QueueService tlqQueueService;
	/***
	 * 
	 * @param data 要传入的数据
	 * @param fromCode 传输通道的起点
	 * @param toCode   传输通道的终点
	 * @param exchageLifecycle 传输的类型
	 * @return
	 */
	public Status sendData(Object data,String fromCode,String toCode,QueueMsgWrapper.ExchageLifecycle exchageLifecycle){
		logger.info("TlqTransportPipeline >>>>>> sending data[" + data + "]");
		Status status = Status.getInstance();
		String msg = "";
		try {
			CmData cmData=null;
			if (data instanceof SendCmData) {
				cmData=(SendCmData)data;
			}else if(data instanceof RecvCmData){
				cmData=(RecvCmData)data;
			}else if(data instanceof CrossNetTmpCmData){
				cmData=(CrossNetTmpCmData)data;
			}else{
				logger.info("TlqTransportPipeline >>>>>> sending data[" + data + "]不能被识别");
				msg = "数据发送至传输管道[TlqTransportPipeline],参数data不能被识别";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			String filePath = AppConst.CMDATA_STORED_DIR + File.separator + cmData.getPayload();
			File file = new File(filePath);
			if (!file.exists()) {
				msg = "数据发送至传输管道[TlqTransportPipeline]失败：数据报文文件[" + filePath + "]找不到";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String payloadContent = FileUtils.readFileToString(file,AppConst.CHARSET_UTF8);
			cmData.setPayloadContent(payloadContent);

			//@TODO:直接跟注册信息关联???
			//注意，队列是按照规则直接拼凑的
			String queueName = "SQ_" + fromCode.toUpperCase() + "_TO_"+ toCode.toUpperCase() + "_CM_DATA";
			QueueMsgWrapper queueMsgWrapper = new QueueMsgWrapper();
			queueMsgWrapper.setQueueName(queueName);
			queueMsgWrapper.setExchageLifecycle(exchageLifecycle);
			queueMsgWrapper.setData(cmData);
			status = tlqQueueService.sendMessage(queueName, queueMsgWrapper);
			if (status.isSuccess()) {
				msg = "数据发送至传输管道成功：[TlqTransportPipeline]" + status.getMsg();
				status.setSuccess(true);
				status.setMsg(msg);
			} else {
				msg = "数据发送至传输管道失败：[TlqTransportPipeline]:" + status.getMsg();
				status.setSuccess(false);
				status.setMsg(msg);
			}
			return status;
		} catch (Exception e) {
			msg = "数据发送至传输管道[TlqTransportPipeline]出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);
			logger.error(msg, e);
			return status;
		}
	}
	
	/***
	 * 
	 * @param data 要传入的数据
	 * @param fromCode 传输通道的起点
	 * @param toCode   传输通道的终点
	 * @param exchageLifecycle 传输的类型
	 * @return
	 */
	public Status sendLog(Object data,String fromCode,String toCode,QueueMsgWrapper.ExchageLifecycle exchageLifecycle){
		logger.info("TlqTransportPipeline >>>>>> sending data[" + data + "]");
		Status status = Status.getInstance();
		String msg = "";
		try {
			CmData cmData=null;
			if (data instanceof SendCmData) {
				cmData=(SendCmData)data;
			}else if(data instanceof RecvCmData){
				cmData=(RecvCmData)data;
			}else if(data instanceof CrossNetTmpCmData){
				cmData=(CrossNetTmpCmData)data;
			}else{
				logger.info("TlqTransportPipeline >>>>>> sending data[" + data + "]不能被识别");
				msg = "数据发送至传输管道[TlqTransportPipeline],参数data不能被识别";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			String filePath = AppConst.CMDATA_STORED_DIR + File.separator + cmData.getPayload();
			File file = new File(filePath);
			if (!file.exists()) {
				msg = "数据发送至传输管道[TlqTransportPipeline]失败：数据报文文件[" + filePath + "]找不到";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String payloadContent = FileUtils.readFileToString(file);
			cmData.setPayloadContent(payloadContent);

			//@TODO:直接跟注册信息关联???
			//注意，队列是按照规则直接拼凑的
			String queueName  = "SQ_" + fromCode.toUpperCase() + "_TO_"+ toCode.toUpperCase()+ "_CM_DATA_LOG";
			QueueMsgWrapper queueMsgWrapper = new QueueMsgWrapper();
			queueMsgWrapper.setQueueName(queueName);
			queueMsgWrapper.setExchageLifecycle(exchageLifecycle);
			queueMsgWrapper.setData(cmData);
			status = tlqQueueService.sendMessage(queueName, queueMsgWrapper);
			if (status.isSuccess()) {
				msg = "数据发送至传输管道成功：[TlqTransportPipeline]" + status.getMsg();
				status.setSuccess(true);
				status.setMsg(msg);
			} else {
				msg = "数据发送至传输管道失败：[TlqTransportPipeline]:" + status.getMsg();
				status.setSuccess(false);
				status.setMsg(msg);
			}
			return status;
		} catch (Exception e) {
			msg = "数据发送至传输管道[TlqTransportPipeline]出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);
			logger.error(msg, e);
			return status;
		}
	}
	
}
